package persist

import (
	"errors"
	"gopkg.in/olivere/elastic.v3"
	"learngo/crawler/engine"
	"log"
)

func ItemSaver(index string) (chan engine.Item, error) {
	// client 如果放在save里的话会创建很多client消耗性能
	client, err := elastic.NewClient(
		// sniff是维护客户端集群状态，因为es跑在docker上所以外部无法看到，该选项必须为false
		// Must turn off sniff in docker
		elastic.SetSniff(false))

	if err != nil {
		return nil, err
	}
	// 通过out和engine进行传递
	out := make(chan engine.Item)
	go func() {
		itemCount := 0
		for {
			item := <-out
			log.Printf("Item Saver: got item"+"#%d: %v", itemCount, item)
			itemCount++

			err := Save(client, index, item)
			if err != nil {
				log.Print("Item Saver: error"+
					"saving item %v: %v", item, err)
			}
		}
	}()
	return out, nil
}

func Save(client *elastic.Client, index string, item engine.Item) error {

	if item.Type == "" {
		return errors.New("must supply Type")
	}

	// Index 存数据，可以创建可以修改取决于id是否存在
	// Index 数据库名，Type 表名
	indexService := client.Index().
		Index(index).
		Type(item.Type).BodyJson(item)
	if item.Id != "" {
		indexService.Id(item.Id)
	}
	_, err := indexService.Do()
	if err != nil {
		return nil
	}
	//fmt.Printf("%+v",resp)
	return nil
}
